﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using BrokerBase.Lib.Person;

namespace BrokerBase.Lib.Client
{
    public class ClientConfiguration
    {
        private const int DEFAULT_BEFOREDAY = 10;

        public const string DEFAULT_FILE_NAME = "defaultconfig.xml";

        public const string USER_FILE_NAME = "config.xml";

        private XElement element;

        public static ClientConfiguration Load(string fileName)
        {
            string tmp = null;
            bool isSuccess = false;

            XElement root = XElement.Load(fileName);

            ClientConfiguration cfg = new ClientConfiguration();

            cfg.DefaultCompany = root.Element("DefaultCompany").Value;

            tmp = root.Element("DefaultBirthday").Value;
            DateTime tmpTime = DateTime.Parse("1980-06-01");
            isSuccess = DateTime.TryParse(tmp, out tmpTime);

            if (isSuccess)
                cfg.DefaultBirthday = tmpTime;
            else
                cfg.DefaultBirthday = DateTime.Parse("1980-06-01");

            tmp = root.Element("DefaultSex").Value;

            if (!string.IsNullOrEmpty(tmp))
                cfg.DefaultSex = tmp;
            else
                cfg.DefaultSex = "女";


            tmp = root.Element("MaxAge").Value;
            int maxAge = 120;
            isSuccess = int.TryParse(tmp, out maxAge);

            if (isSuccess)
                cfg.MaxAge = maxAge;
            else
                cfg.MaxAge = 120;

            tmp = root.Element("BeforeBirthday").Value;
            int bb = 15;
            isSuccess = int.TryParse(tmp, out bb);

            if (isSuccess)
                cfg.BeforeBirthday = bb;
            else
                cfg.BeforeBirthday = DEFAULT_BEFOREDAY;

            tmp = root.Element("BeforePayment").Value;
            int bp = 15;
            isSuccess = int.TryParse(tmp, out bp);
            if (isSuccess)
                cfg.BeforePayment = bp;
            else
                cfg.BeforePayment = DEFAULT_BEFOREDAY;

            cfg.ServerUrl1 = root.Element("ServerUrl1").Value;

            cfg.ServerUrl2 = root.Element("ServerUrl2").Value;

            XElement xel = root.Element("IsLogin");

            if(xel!=null)
                cfg.IsLogin = Convert.ToBoolean(xel.Value);
            
            cfg.element = root;

            return cfg;
        }

        public static void Save(ClientConfiguration model, string fileName)
        {
            model.element.SetElementValue("DefaultCompany", model.DefaultCompany);

            model.element.SetElementValue("DefaultBirthday", model.DefaultBirthday.ToString("yyyy-MM-dd"));

            model.element.SetElementValue("DefaultSex", model.DefaultSex);

            model.element.SetElementValue("MaxAge", model.MaxAge);

            model.element.SetElementValue("BeforeBirthday", model.BeforeBirthday);

            model.element.SetElementValue("BeforePayment", model.BeforePayment);

            model.element.SetElementValue("ServerUrl1", model.ServerUrl1);

            model.element.SetElementValue("ServerUrl2", model.ServerUrl2);

            model.element.SetElementValue("IsLogin", model.IsLogin);

            model.element.Save(fileName);
        }

        public string DefaultCompany { get; set; }

        public DateTime DefaultBirthday { get; set; }

        public string DefaultSex { get; set; }

        public int MaxAge { get; set; }

        public int BeforeBirthday { get; set; }

        public int BeforePayment { get; set; }

        public string ServerUrl1 { get; set; } //线程安全否？

        public string ServerUrl2 { get; set; }

        public bool IsLogin { get; set; }

        public string DataServiceUrl
        {
            get
            {
                return ServerUrl1.TrimEnd("/".ToCharArray()) + "/" + "DataService.svc";
            }
        }

        public string LicenseUrl
        {
            get
            {
                return ServerUrl1.TrimEnd("/".ToCharArray()) + "/" + "LicenseService.svc";
            }
        }

        public string DataUpdateUrl
        {
            get
            {
                return ServerUrl1.TrimEnd("/".ToCharArray()) + "/" + "DataUpdateService.svc";
            }
        }
    }
}
